package com.useeinfo.oa.open.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.func.lang.StringUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Organization;
import com.useeinfo.oa.modules.security.biz.OrganizationBiz;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/organization")
public class OrganizationAPI {
    @Autowired
    private OrganizationBiz organizationBiz;
    /**
     * 获取组织列表
     */
    @RequestMapping(value = "/getOrganizationList")
    public ResultDto getOrganizationList(@RequestParam(value = "pageIndex", required = false) String pageIndexParam,
                                         @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                         HttpServletRequest request){
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        QueryParam queryParam = new QueryParam(pageIndexParam, pageSizeParam);
        if(enterprise!=null){
            queryParam.getSqlMap().put("enterpriseId",enterprise.getId()+"");
        }
        queryParam.setSqlOrder(" order by o.lvl asc");
        List<Organization> organizationList = organizationBiz.findList(queryParam);
        JSONArray array = new JSONArray();
        for (Organization organization : organizationList){
            JSONObject json = new JSONObject();
            json.put("organizationName",organization.getOrganizationName());
            json.put("organizationId",organization.getId());
            String pathName = "";
            if(StringUtils.isNotBlank(organization.getParentIds())){
                String parentIds[] = organization.getParentIds().split(",");
                for (int i = 0; i < parentIds.length; i++) {
                    if(StringUtils.isNotBlank(parentIds[i])){
                        Organization pathOrganization = organizationBiz.findModel(StringConverters.ToLong(parentIds[i]));
                        if(pathOrganization!=null){
                            pathName+=pathOrganization.getOrganizationName()+"-";
                        }
                    }
                }
            }
            if(pathName.endsWith("-")){
                pathName = pathName.substring(0,pathName.length()-1);
            }
            json.put("pathName",pathName);
            array.add(json);
        }
        return ResultUtil.success(array);
    }
}
